package weaver.interfaces.zhlx.duss.action.financemodule.t6;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.T6Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 资金计划提报审批流程提交归档时调用接口推送资金系统
 * @ModifyLog:
 **/
public class WeaZjjhtbsplcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = Math.abs(requestInfo.getRequestManager().getFormid());
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaZjjhtbsplcAction>>>>>requestid=" + requestid + ",formid=" + formid);
        try {
            String message = "调用资金系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String sfycmx1 = Util.null2String(rs.getString("sfycmx1"));
            String sfycmx2 = Util.null2String(rs.getString("sfycmx2"));
            String sfycmx3 = Util.null2String(rs.getString("sfycmx3"));
            //明细表1
            if (!"0".equals(sfycmx1)) {
                JSONArray dataArry1 = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt1 where mainid=? and nvl(sftqzf,1)=1 and nvl(type,1)=1 and nvl(cwfhjeyb,0)>0", mainid);
                while (rs.next()) {
                    JSONObject dataJson = new JSONObject();
                    //计划ID
                    dataJson.put("DETAIL_ID", Util.null2String(rs.getString("jhid")));
                    //来源
                    dataJson.put("DATA_SOURCE", "EC");
                    String ly = Util.null2String(rs.getString("ly"));
                    if ("1".equals(ly) || "3".equals(ly) || "5".equals(ly)) {
                        dataJson.put("CD_SIGN", "0");
                    }
                    if ("0".equals(ly) || "2".equals(ly) || "4".equals(ly) || "6".equals(ly)) {
                        dataJson.put("CD_SIGN", "1");
                    }
                    //资金明细项目
                    dataJson.put("DETAIL_TEXT", Util.null2String(rs.getString("zjmxxmwb")));
                    //预算编码
                    dataJson.put("ITEM_CODE", Util.null2String(rs.getString("ysbm")));
                    //预算科目
                    dataJson.put("ITEM_NAME", Util.null2String(rs.getString("ysxm")));
                    //公司代码
                    dataJson.put("CORP_CODE", Util.null2String(rs.getString("gsdm")));
                    //公司名称
                    dataJson.put("CORP_NAME", Util.null2String(rs.getString("gsmc")));
                    //客商编码
                    dataJson.put("CS_CODE", Util.null2String(rs.getString("ksbm")));
                    //客商名称
                    dataJson.put("CS_NAME", Util.null2String(rs.getString("ksmc")));
                    //支付方式
                    String zffs = Util.null2String(rs.getString("zffs"));
                    String voucherType = "0".equals(zffs) ? "06" : "70";
                    dataJson.put("VOUCHER_TYPE", voucherType);
                    //币种
                    dataJson.put("CUR", Util.null2String(rs.getString("bz")));
                    //交易支付内容
                    dataJson.put("ABS", Util.null2String(rs.getString("jyzfnr")));
                    //业务确认人
                    dataJson.put("BUSINESS_USER", Util.null2String(rs.getString("ywqrrwb")));
                    //填报部门
                    dataJson.put("DEPT_NAME", Util.null2String(rs.getString("tbbmwb")));
                    //填报部门编码
                    dataJson.put("DEPT_CODE", Util.null2String(rs.getString("tbbmbm")));
                    //财务复核金额（原币）
                    dataJson.put("AMOUNT_Y", Util.null2String(rs.getString("cwfhjeyb")));
                    //财务复核金额（本币）
                    dataJson.put("AMOUNT_B", Util.null2String(rs.getString("cwfhjebb")));
                    //开户银行（电汇）
                    dataJson.put("PAYEE_BANK1", Util.null2String(rs.getString("khyx")));
                    //联行号（电汇）
                    dataJson.put("PAYEE_BANK_CODE1", Util.null2String(rs.getString("lxh")));
                    //银行账号（电汇）
                    dataJson.put("PAYEE_ACC_NO1", Util.null2String(rs.getString("yxzhdh")));
                    //开户银行（承兑）
                    dataJson.put("PAYEE_BANK2", Util.null2String(rs.getString("khyx1")));
                    //联行号（承兑）
                    dataJson.put("PAYEE_BANK_CODE2", Util.null2String(rs.getString("lxh1")));
                    //银行账号（承兑）
                    dataJson.put("PAYEE_ACC_NO2", Util.null2String(rs.getString("yxzhcd")));
                    //批量/单笔
                    dataJson.put("IS_BATCH", Util.null2String(rs.getString("pldb")));
                    dataArry1.add(dataJson);
                }
                if (!dataArry1.isEmpty()) {
                    Map<String, Object> dataMap = new HashMap<>();
                    dataMap.put("jkdymc", "WeaZjjhtbsplcAction");
                    dataMap.put("lcid", requestid);
                    dataMap.put("dylc", requestid);
                    dataMap.put("cz", src);
                    writeLog(">>>>>WeaZjjhtbsplcAction>>>>>dataArry1=" + dataArry1);
                    JSONObject resultJson = T6Util.saveFundingPlan(dataArry1, dataMap);
                    writeLog(">>>>>WeaZjjhtbsplcAction>>>>>resultJson=" + resultJson);
                    String code = Util.null2String(resultJson.getString("code"));
                    if ("success".equals(code)) {
                        JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                        message += Util.null2String(retrunData.getString("MESSAGE"));
                        String data = Util.null2String(retrunData.getString("DATA"));
                        JSONArray dataList = JSONArray.parseArray(data);
                        int count = 0;
                        for (int i = 0; i < dataList.size(); i++) {
                            JSONObject dataJson = dataList.getJSONObject(i);
                            String code1 = Util.null2String(dataJson.getString("CODE"));
                            String msg = Util.null2String(dataJson.getString("MSG"));
                            String serialId = Util.null2String(dataJson.getString("SERIAL_ID"));
                            if (!"2".equals(code1)) {
                                message += msg + ";";
                                count++;
                            } else {
                                rs.executeUpdate("update " + tablename + "_dt1 set type=0 where jhid=?", serialId);
                            }
                        }
                        if (count > 0) {
                            requestInfo.getRequestManager().setMessageid("111111");
                            requestInfo.getRequestManager().setMessagecontent(message);
                            return Action.FAILURE_AND_CONTINUE;
                        } else {
                            rs.executeUpdate("update " + tablename + " set sfycmx1=0 where requestid=?", requestid);
                        }
                    } else {
                        message += Util.null2String(resultJson.getString("message"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            //明细表2
            if (!"0".equals(sfycmx2)) {
                JSONArray dataArry2 = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt2 where mainid=? and nvl(sftqzf,1)=1 and nvl(type,1)=1 and nvl(cwfhjeyb,0)>0", mainid);
                while (rs.next()) {
                    JSONObject dataJson = new JSONObject();
                    //计划ID
                    dataJson.put("DETAIL_ID", Util.null2String(rs.getString("jhid")));
                    //来源
                    dataJson.put("DATA_SOURCE", "EC");
                    String ly = Util.null2String(rs.getString("ly"));
                    if ("1".equals(ly) || "3".equals(ly) || "5".equals(ly)) {
                        dataJson.put("CD_SIGN", "0");
                    }
                    if ("0".equals(ly) || "2".equals(ly) || "4".equals(ly) || "6".equals(ly)) {
                        dataJson.put("CD_SIGN", "1");
                    }
                    //资金明细项目
                    dataJson.put("DETAIL_TEXT", Util.null2String(rs.getString("zjmxxmwb")));
                    //预算编码
                    dataJson.put("ITEM_CODE", Util.null2String(rs.getString("ysbm")));
                    //预算科目
                    dataJson.put("ITEM_NAME", Util.null2String(rs.getString("ysxm")));
                    //公司代码
                    dataJson.put("CORP_CODE", Util.null2String(rs.getString("gsdm")));
                    //公司名称
                    dataJson.put("CORP_NAME", Util.null2String(rs.getString("gsmcwb")));
                    //客商编码
                    dataJson.put("CS_CODE", Util.null2String(rs.getString("ksbm")));
                    //客商名称
                    dataJson.put("CS_NAME", Util.null2String(rs.getString("ksmc")));
                    //支付方式
                    String zffs = Util.null2String(rs.getString("zffs"));
                    String voucherType = "0".equals(zffs) ? "06" : "70";
                    dataJson.put("VOUCHER_TYPE", voucherType);
                    //币种
                    dataJson.put("CUR", Util.null2String(rs.getString("bzwb")));
                    //交易支付内容
                    dataJson.put("ABS", Util.null2String(rs.getString("jyzfnr")));
                    //业务确认人
                    dataJson.put("BUSINESS_USER", Util.null2String(rs.getString("ywqrrwb")));
                    //填报部门
                    dataJson.put("DEPT_NAME", Util.null2String(rs.getString("tbbmwb")));
                    //填报部门
                    dataJson.put("DEPT_CODE", Util.null2String(rs.getString("tbbmbm")));
                    //财务复核金额（原币）
                    dataJson.put("AMOUNT_Y", Util.null2String(rs.getString("cwfhjeyb")));
                    //财务复核金额（本币）
                    dataJson.put("AMOUNT_B", Util.null2String(rs.getString("cwfhjebb")));
                    //开户银行（电汇）
                    dataJson.put("PAYEE_BANK1", Util.null2String(rs.getString("khyx")));
                    //联行号（电汇）
                    dataJson.put("PAYEE_BANK_CODE1", Util.null2String(rs.getString("lxh")));
                    //银行账号（电汇）
                    dataJson.put("PAYEE_ACC_NO1", Util.null2String(rs.getString("yxzhdh")));
                    //开户银行（承兑）
                    dataJson.put("PAYEE_BANK2", Util.null2String(rs.getString("khyx1")));
                    //联行号（承兑）
                    dataJson.put("PAYEE_BANK_CODE2", Util.null2String(rs.getString("lxh1")));
                    //银行账号（承兑）
                    dataJson.put("PAYEE_ACC_NO2", Util.null2String(rs.getString("yxzhcd")));
                    //批量/单笔
                    dataJson.put("IS_BATCH", Util.null2String(rs.getString("pldb")));
                    dataArry2.add(dataJson);
                }
                if (!dataArry2.isEmpty()) {
                    Map<String, Object> dataMap = new HashMap<>();
                    dataMap.put("jkdymc", "WeaZjjhtbsplcAction");
                    dataMap.put("lcid", requestid);
                    dataMap.put("dylc", requestid);
                    dataMap.put("cz", src);
                    writeLog(">>>>>WeaZjjhtbsplcAction>>>>>dataArry2=" + dataArry2);
                    JSONObject resultJson = T6Util.saveFundingPlan(dataArry2, dataMap);
                    writeLog(">>>>>WeaZjjhtbsplcAction>>>>>resultJson=" + resultJson);
                    String code = Util.null2String(resultJson.getString("code"));
                    if ("success".equals(code)) {
                        JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                        message += Util.null2String(retrunData.getString("MESSAGE"));
                        String data = Util.null2String(retrunData.getString("DATA"));
                        JSONArray dataList = JSONArray.parseArray(data);
                        int count = 0;
                        for (int i = 0; i < dataList.size(); i++) {
                            JSONObject dataJson = dataList.getJSONObject(i);
                            String code1 = Util.null2String(dataJson.getString("CODE"));
                            String msg = Util.null2String(dataJson.getString("MSG"));
                            String serialId = Util.null2String(dataJson.getString("SERIAL_ID"));
                            if (!"2".equals(code1)) {
                                message += msg + ";";
                                count++;
                            } else {
                                rs.executeUpdate("update " + tablename + "_dt2 set type=0 where jhid=?", serialId);
                            }
                        }
                        if (count > 0) {
                            requestInfo.getRequestManager().setMessageid("111111");
                            requestInfo.getRequestManager().setMessagecontent(message);
                            return Action.FAILURE_AND_CONTINUE;
                        } else {
                            rs.executeUpdate("update " + tablename + " set sfycmx2=0 where requestid=?", requestid);
                        }
                    } else {
                        message += Util.null2String(resultJson.getString("message"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            //明细表3
            if (!"0".equals(sfycmx3)) {
                JSONArray dataArry3 = new JSONArray();
                rs.executeQuery("select * from " + tablename + "_dt3 where mainid=? and nvl(sftqzf,1)=1 and nvl(type,1)=1 and nvl(cwfhjeyb,0)>0", mainid);
                while (rs.next()) {
                    JSONObject dataJson = new JSONObject();
                    //计划ID
                    dataJson.put("DETAIL_ID", Util.null2String(rs.getString("jhid")));
                    //来源
                    dataJson.put("DATA_SOURCE", "EC");
                    String ly = Util.null2String(rs.getString("ly"));
                    if ("1".equals(ly) || "3".equals(ly) || "5".equals(ly)) {
                        dataJson.put("CD_SIGN", "0");
                    }
                    if ("0".equals(ly) || "2".equals(ly) || "4".equals(ly) || "6".equals(ly)) {
                        dataJson.put("CD_SIGN", "1");
                    }
                    //资金明细项目
                    dataJson.put("DETAIL_TEXT", Util.null2String(rs.getString("zjmxxm")));
                    //预算编码
                    dataJson.put("ITEM_CODE", Util.null2String(rs.getString("ysbm")));
                    //预算科目
                    dataJson.put("ITEM_NAME", Util.null2String(rs.getString("ysxm")));
                    //公司代码
                    dataJson.put("CORP_CODE", Util.null2String(rs.getString("gsdm")));
                    //公司名称
                    dataJson.put("CORP_NAME", Util.null2String(rs.getString("gsmc")));
                    //客商编码
                    dataJson.put("CS_CODE", Util.null2String(rs.getString("ksbm")));
                    //客商名称
                    dataJson.put("CS_NAME", Util.null2String(rs.getString("ksmc")));
                    //支付方式
                    String zffs = Util.null2String(rs.getString("zffs"));
                    String voucherType = "0".equals(zffs) ? "06" : "70";
                    dataJson.put("VOUCHER_TYPE", voucherType);
                    //币种
                    dataJson.put("CUR", Util.null2String(rs.getString("bz")));
                    //交易支付内容
                    dataJson.put("ABS", Util.null2String(rs.getString("jyzfnr")));
                    //业务确认人
                    dataJson.put("BUSINESS_USER", Util.null2String(rs.getString("ywqrrwb")));
                    //填报部门
                    dataJson.put("DEPT_NAME", Util.null2String(rs.getString("tbbmwb")));
                    //填报部门
                    dataJson.put("DEPT_CODE", Util.null2String(rs.getString("tbbmbm")));
                    //财务复核金额（原币）
                    dataJson.put("AMOUNT_Y", Util.null2String(rs.getString("cwfhjeyb")));
                    //财务复核金额（本币）
                    dataJson.put("AMOUNT_B", Util.null2String(rs.getString("cwfhjebb")));
                    //开户银行（电汇）
                    dataJson.put("PAYEE_BANK1", Util.null2String(rs.getString("khyx")));
                    //联行号（电汇）
                    dataJson.put("PAYEE_BANK_CODE1", Util.null2String(rs.getString("lxh")));
                    //银行账号（电汇）
                    dataJson.put("PAYEE_ACC_NO1", Util.null2String(rs.getString("yxzhdh")));
                    //开户银行（承兑）
                    dataJson.put("PAYEE_BANK2", Util.null2String(rs.getString("khyx1")));
                    //联行号（承兑）
                    dataJson.put("PAYEE_BANK_CODE2", Util.null2String(rs.getString("lxh1")));
                    //银行账号（承兑）
                    dataJson.put("PAYEE_ACC_NO2", Util.null2String(rs.getString("yxzhcd")));
                    //批量/单笔
                    dataJson.put("IS_BATCH", Util.null2String(rs.getString("pldb")));
                    dataArry3.add(dataJson);
                }
                if (!dataArry3.isEmpty()) {
                    Map<String, Object> dataMap = new HashMap<>();
                    dataMap.put("jkdymc", "WeaZjjhtbsplcAction");
                    dataMap.put("lcid", requestid);
                    dataMap.put("dylc", requestid);
                    dataMap.put("cz", src);
                    writeLog(">>>>>WeaZjjhtbsplcAction>>>>>dataArry3=" + dataArry3);
                    JSONObject resultJson = T6Util.saveFundingPlan(dataArry3, dataMap);
                    writeLog(">>>>>WeaZjjhtbsplcAction>>>>>resultJson=" + resultJson);
                    String code = Util.null2String(resultJson.getString("code"));
                    if ("success".equals(code)) {
                        JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                        message += Util.null2String(retrunData.getString("MESSAGE"));
                        String data = Util.null2String(retrunData.getString("DATA"));
                        JSONArray dataList = JSONArray.parseArray(data);
                        int count = 0;
                        for (int i = 0; i < dataList.size(); i++) {
                            JSONObject dataJson = dataList.getJSONObject(i);
                            String code1 = Util.null2String(dataJson.getString("CODE"));
                            String msg = Util.null2String(dataJson.getString("MSG"));
                            String serialId = Util.null2String(dataJson.getString("SERIAL_ID"));
                            if (!"2".equals(code1)) {
                                message += msg + ";";
                                count++;
                            } else {
                                rs.executeUpdate("update " + tablename + "_dt3 set type=0 where jhid=?", serialId);
                            }
                        }
                        if (count > 0) {
                            requestInfo.getRequestManager().setMessageid("111111");
                            requestInfo.getRequestManager().setMessagecontent(message);
                            return Action.FAILURE_AND_CONTINUE;
                        } else {
                            rs.executeUpdate("update " + tablename + " set sfycmx3=0 where requestid=?", requestid);
                        }
                    } else {
                        message += Util.null2String(resultJson.getString("message"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }

                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZjjhtbsplcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}